<?php

error_reporting (E_ALL ^ E_NOTICE);

if(!empty($_FILES)) 
{
include 'config.php';

$file = $_FILES['image_file'];
$file_name = $file['name'];
$error = ''; // Empty

// Get File Extension (if any)
$ext = strtolower(substr(strrchr($file_name, "."), 1));

// Check for a correct extension. The image file hasn't an extension? Add one
   if($validation_type == 1)
   {
   $file_info = getimagesize($_FILES['image_file']['tmp_name']);

      if(empty($file_info))
      {
      $error .= "The uploaded file doesn't seem to be an image.";
      }
      else
      {
      $file_mime = $file_info['mime'];

         if($ext == 'jpc' || $ext == 'jpx' || $ext == 'jb2')
         {
         $extension = $ext;
         }
         else
         {
         $extension = ($mime[$file_mime] == 'jpeg') ? 'jpg' : $mime[$file_mime];
         }

         if(!$extension)
         {
         $extension = '';
         $file_name = str_replace('.', '', $file_name);
         }
	  }
   }
   else if($validation_type == 2)
   {
	  if(!in_array($ext, $image_extensions_allowed))
	  {
	  $exts = implode(', ',$image_extensions_allowed);
	  $error .= "You must upload a file with one of the following extensions: ".$exts;
	  }

	  $extension = $ext;
   }

   if($error == "") // No errors were found?
   {
   $new_file_name = strtolower($file_name);
   $new_file_name = str_replace(' ', '-', $new_file_name);
   $new_file_name = substr($new_file_name, 0, -strlen($ext));
   $new_file_name .= $extension;


   $move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$new_file_name);

   if($move_file)
	   {
	   $done = true;
	
		$con = mysql_connect($dbHost,$username,$password);
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }

		mysql_select_db($dbName, $con);
		$query = sprintf("INSERT INTO uc_images (fileName) VALUES ('%s')", mysql_real_escape_string($new_file_name));
		$result = mysql_query($query);
		mysql_close($con);

	   }
   	}
   else
   {
   @unlink($file['tmp_name']);
   }
   $file_uploaded = true;
} 

else 
{
	if ($_GET["source"] == "picup") {
		$picup = true;
		$done = true;
	}
}

// The file
//$filename = $upload_image_to_folder.$new_file_name;
//list($width, $height) = getimagesize($filename);
//if ($width > 1024) {
//	$percent = 1024 / $width;
//}

//list($width, $height) = getimagesize($filename);
//$new_width = $width * $percent;
//$new_height = $height * $percent;

// Resample
//$image_p = imagecreatetruecolor($new_width, $new_height);
//$image = imagecreatefromjpeg($filename);
//imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Output
//imagejpeg($image_p, null, 100);

if($file_uploaded || $picup)
{
	if($done)
	{
		$url = 'Location: http://goodman.se/rateme/done.php';
		header( $url ) ;
	}
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Rate Me!</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width; initial-scale=0.9; maximum-scale=1.0; user-scalable=0;" />
</head>

<body>

<div id="uploadForm">
<h1>Rate Me!</h1>
<h3><?php

		echo $error;
?></h3>
<a href = "http://goodman.se/rateme/">Go back</a>
</div>

</body>
</html>
